﻿using System.Text.Json;
using System.Text;
using System.Net.Mime;
using System.Net;

namespace RndMaterialDisposalService.Helper
{
    /// <summary>
    /// 
    /// </summary>
    public class HttpHelper
    {
        private static readonly string contentType = "application/json";
        private readonly ILogger<HttpHelper> logger;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_logger"></param>
        public HttpHelper(ILogger<HttpHelper> _logger)
        {
            logger = _logger;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="json"></param>
        /// <returns></returns>
        public async static Task<string> PostAsync(string url, string json)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(contentType));
                var data = new StringContent(json, Encoding.UTF8, contentType);
                var task = await client.PostAsync(url, data);
                var result = await task.Content.ReadAsStringAsync();
                if ((int)task.StatusCode >= 400)
                {
                    throw new Exception(result);
                }
                return result;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="json"></param>
        /// <returns></returns>
        public async static Task<T> PostAsync<T>(string url, string json)
        {
            var result = await PostAsync(url, json);
            if (string.IsNullOrEmpty(result))
            {
                return default(T)!;
            }
            var JsonOptions = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true
            };
            return JsonSerializer.Deserialize<T>(result, JsonOptions)!;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public async static Task<string> GetAsync(string url)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(contentType));
                var task = await client.GetAsync(url);
                var result = await task.Content.ReadAsStringAsync();
                if ((int)task.StatusCode >= 400)
                {
                    throw new Exception(result);
                }
                //logger.LogTrace(result);
                return result;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <returns></returns>
        public async static Task<T> GetAsync<T>(string url)
        {
            var result = await GetAsync(url);
            if (string.IsNullOrEmpty(result))
            {
                return default(T)!;
            }
            var JsonOptions = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true
            };
            return JsonSerializer.Deserialize<T>(result, JsonOptions)!;
        }
    }
}
